<?php
include_once( "./sql/sqlCon.php" );
include_once( "newsCMS/newsCMSConfigs.php" );

$refPage = "newsCMSUpdater.php";
include( "langincluder.php" );


if( !isset($_SESSION[$sessionUserLevelName]) || $_SESSION[$sessionUserLevelName]!=1 )
{
echo 'login fun';

}
else // we have admin rights
{
	$logid = (int)$_GET['lid'];
	if( isset($_GET['action']) && $_GET['action'] == 'update' ) // actually do the update
	{
		if( !isset($_POST['logtitle']) || empty($_POST['logtitle']) || !isset($_POST['logcontent']) || empty($_POST['logcontent']) ) 
		{
			echo '<div style="text-align: center">'.$strings['fieldNotFilledError'].
				 '<br><br><button onclick="history.back()">'.$strings['goBack'].'</button></div>';
		}
		else
		{
		
			$mcstate = (bool)get_magic_quotes_gpc();
			$title = htmlspecialchars(substr($mcstate ? $_POST['logtitle'] : mysql_real_escape_string($_POST['logtitle']), 0, $maxTitleSize));
			$content = htmlspecialchars(substr($mcstate ? $_POST['logcontent'] : mysql_real_escape_string($_POST['logcontent']), 0, $maxContentSize));
			$link = htmlspecialchars(substr($mcstate ? $_POST['loglink'] : mysql_real_escape_string($_POST['loglink']), 0, $maxLinkSize));

			mysql_query("UPDATE $newsCMSTableName SET title='$title', content='$content', discussl='$link' WHERE line='$logid'");
			if( !mysql_error() )
				echo $strings['successRedirecting'].'<script type="text/javascript">setTimeout(\'window.location = "./"\', 3000);</script>';
			else 
				echo mysql_error();
		}
	}
	else // show the edit gui
	{
		$quer = mysql_query("SELECT * FROM $newsCMSTableName WHERE line = $logid");
		$result = mysql_fetch_array($quer);
		if(!isset($result['line'])) 
			echo $strings['entryIDError']." ".$logid.'<br><button onclick="history.back()">'.$strings['goBack'].'</button>';
		else
		{
?>


<script type='text/javascript'>
function checklength()
{
	var contentmaxsize = <?php echo $maxContentSize; ?>;
	var textform = document.getElementById('logform');
	var content = textform.contentarea.value;
	if(content.length>contentmaxsize)
	{
		textform.contentarea.value = content.substr(0, contentmaxsize);
		alert('<?php echo $strings['charLimitReached']; ?>');
	}
	document.getElementById('remchars').innerHTML = contentmaxsize-content.length;
}



var emptyErrorBGColor = "#ffaaaa";
var defaultColor = "unset";

var watermarkElementsID = new Array('titleInp', 'contentarea', 'discussionInp');
var watermarks = new Array(<?php echo "'".$strings['titleInputWatermark']."', '".$strings['contentAreaWatermark']."', '".$strings['discussionInputWatermark']."'"; ?>);
if( typeof(watermarkColor) == "undefined" ) var watermarkColor = "#ff0000";

function trigMe(a)
{
	//focus blur
	var i=-1;
	for (ii in watermarkElementsID)
	{
		if( watermarkElementsID[ii] == a ) i = ii;
	}
	
	var element = document.getElementById(a);
	if( i!= -1 )
	{
		if( element.value.length == 0 ) setWatermark(element, i);
		else
		{	
			if( element.value == watermarks[i] ) unsetWatermark(element);
			else if( defaultColor != 'unset' ) element.style.backgroundColor = defaultColor;
		}
	}
}
function setWatermark(element, t) 
{
	element.value = watermarks[t];
	element.style.color = watermarkColor;
}
function unsetWatermark(element) 
{
	element.value = '';
	element.style.color = "inherit";
}
function placeAllWatermarks()
{
	for(var a in watermarkElementsID) trigMe(watermarkElementsID[a]);
}
function submitIt()
{
	for (ii in watermarkElementsID)
	{
		var element = document.getElementById(watermarkElementsID[ii]);
		if( element.value == watermarks[ii] ) element.value='';	
	}
	if( document.getElementById('titleInp').value.length == 0 )
	{ 
		defaultColor = document.getElementById('titleInp').style.backgroundColor;
		document.getElementById('titleInp').style.backgroundColor = emptyErrorBGColor;
		placeAllWatermarks();
		return;
	}
	if( document.getElementById('contentarea').value.length == 0 )
	{
		defaultColor = document.getElementById('contentarea').style.backgroundColor;
		document.getElementById('contentarea').style.backgroundColor = emptyErrorBGColor;
		placeAllWatermarks();
		return;
	}
	document.forms["logform"].submit();
}
</script>
		   
<div style="text-align : center;">
<form id='logform' method='post' action='index.php?p=602&lid=<?php echo $logid; ?>&action=update'>

<input id="titleInp" onfocus="trigMe('titleInp')" onblur="trigMe('titleInp')" type='text' name='logtitle' size='60' maxlength='<?php echo $maxTitleSize; ?>' value="<?php echo $result['title']; ?>" >

<br><br>
<input id='discussionInp' onfocus="trigMe('discussionInp')" onblur="trigMe('discussionInp')" type='text' name='loglink' size='60' maxlength='<?php echo $maxLinkSize; ?>' value='<?php echo $result['discussl']; ?>' >

<br><br>
<textarea id='contentarea' onfocus="trigMe('contentarea')" onblur="trigMe('contentarea')" name='logcontent' rows='7' cols='46' onfocus='javascript:checklength()' onblur='javascript:checklength()' onkeydown='javascript:checklength()' onkeyup='javascript:checklength()' onchange='javascript:checklength()' onkeypress='javascript:checklength()'><?php echo $result['content']; ?></textarea>

<br>
<?php echo $strings['charsRemaining']."<span id='remchars'>".($maxContentSize-strlen($result['content'])); ?></span>

</form>
<br>
<button type="button" onclick="submitIt()"><?php echo $strings['submitButton']; ?></button>

</div>

<script type='text/javascript'>
placeAllWatermarks();
</script>

<?php
		}
	}
}
?>